﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects.SqlClient;
using System.Linq.Expressions;
using System.Data.Metadata.Edm;

namespace XBoshiGFS.forms
{
    public partial class GroupForm : Form
    {
        //public StationSet station { get; set; }
        //public SegmentCourseSet segment { get; set; }
        //public WaterCourseSet waterCourse { get; set; }
        //public VillageSet village { get; set; }
        public GroupSet group { get; set; }

        public XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();  


        public GroupForm()
        {
            InitializeComponent();


            group = new GroupSet();
            group.Id = Guid.NewGuid().ToString();
            group.strName = "新建组";
            context.AddToGroupSet(group);
            groupSetBindingSource.DataSource = group;
            cmbStation.DataSource = (from p in context.StationSet select p);


        }

        public GroupForm(VillageSet p_village)
        {
            InitializeComponent();


            var village = (from p in context.VillageSet where p.Id == p_village.Id select p).FirstOrDefault();
            group = new GroupSet();
            group.Id = Guid.NewGuid().ToString();
            group.strName = "新建组";
            group.VillageSet = village; 
            context.AddToGroupSet(group);
            groupSetBindingSource.DataSource = group;

            cmbVillage.DataSource = new List<Object>() { village };
            cmbVillage.Enabled = false;
            cmbVillage.SelectedValue = village.Id;


            var watercourse = village.WaterCourseSet;
            cmbWaterCourse.DataSource = new List<Object>() { watercourse };
            cmbWaterCourse.Enabled = false;
            cmbWaterCourse.SelectedValue = watercourse.Id;

            var segment = watercourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;

            //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }

        public GroupForm(GroupSet p_group)
        {
            InitializeComponent();


            group = (from p in context.GroupSet  where p.Id == p_group.Id select p).FirstOrDefault();

            groupSetBindingSource.DataSource = group;
            context.Attach(group);

            var village = group.VillageSet;
            cmbVillage.DataSource = new List<Object>() { village };
            cmbVillage.Enabled = false;
            cmbVillage.SelectedValue = village.Id;


            var watercourse = village.WaterCourseSet;
            cmbWaterCourse.DataSource = new List<Object>() { watercourse };
            cmbWaterCourse.Enabled = false;
            cmbWaterCourse.SelectedValue = watercourse.Id;

            var segment = watercourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;


            //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }

        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {


            var strID = ((ComboBox)sender).SelectedValue;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName select p);


        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbWaterCourse.DataSource = (from p in context.WaterCourseSet where p.SegmentCourseId == strID orderby p.strName select p);

        }

        private void cmbWaterCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbVillage.DataSource = (from p in context.VillageSet where p.WaterCourseId == strID orderby p.strName select p);

        }


    
        private void btnOK_Click(object sender, EventArgs e)
        {

            if (TBName.Text == "") {
                MessageBox.Show("请填写名称！","提示");
                TBName.Focus();
                  return;
            }
            if (TBCode.Text == "") {
                MessageBox.Show("请填写代码！", "提示");
                TBCode.Focus();
                return;
            }
            if (String.IsNullOrWhiteSpace(group.VillageId ))
            {
                MessageBox.Show("请选择上层单位！", "提示");
                return;
            }


            var code = (from p in context.GroupSet where p.strCode == TBCode.Text select p);
            var maxCode="";
            try
            {
                var sugesstNo = (from p in context.GroupSet select p.strCode).Cast<Int32>().Max();
                maxCode =String.Format("{0}",(sugesstNo + 1)); //(int.Parse(sugest.FirstOrDefault().ToString()) + 1).ToString();
            }
            catch { 
            } 
                if (code.Count() == 0)
                {
                }
                else
                {
                    if (code.Count() == 1 && code.First().Id == group.Id)
                    {
                    }
                    else
                    {
                        MessageBox.Show("代码重复！建议编码为：" + maxCode, "提示");
                        TBCode.Focus();
                        return;
                    }
                }
 
            //设置sc的相关信息
            if (group.EntityKey == null)
            {
                context.GroupSet.AddObject(group);
            }
            else
            {
             //   segment.Id = Guid.NewGuid().ToString(); 
             //    context.SegmentCourseSet.Attach(segment);
            }
            context.SaveChanges();
            this.DialogResult = DialogResult.Yes; 
        }

        private void VillageForm_Load(object sender, EventArgs e)
        {

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbVillage_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            group.VillageId = strID.ToString() ; 
        }


    }
}
